package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dao.MemberMapper;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderSettingMapper orderSettingMaper;
    /**
     * 根据id查询预约信息
     * @param id
     * @return
     */
    public OrderInformation findById(Integer id) {
        OrderInformation orderInformation = new OrderInformation();
        Order order =orderMapper.findById(id);
        orderInformation.setOrderType(order.getOrderType());
        orderInformation.setOrderDate(order.getOrderDate());
        Integer setmealId = order.getSetmealId();
        orderInformation.setSetmeal(setmealMapper.getSetmealNameById(setmealId));
        orderInformation.setMember(memberMapper.findById(order.getMemberId()));
        return orderInformation;
    }

    /**
     * 体验预约
     * @param submitOrder
     * @return
     */
    public Result submitOrder(SubmitOrder submitOrder) {
        // 2.校检用户输入的验证码是否正确
        String validateCode = submitOrder.getValidateCode();
        if (validateCode == null || validateCode.length() != 4){
            return new Result(false, "验证码不合法");
        }
        // 从redis中获取验证码
        String code = (String) redisTemplate.opsForValue().get(submitOrder.getTelephone());
        if (code == null || !code.equals(validateCode)){
            return new Result(false, "验证码错误");
        }
        // 3.预约日期的预约人数没有设置的话不能预约
        Integer number = orderSettingMaper.getNumberByOrderDate(submitOrder.getOrderDate());
        if (number == null){
            return new Result(false, "预约日期的预约人数没有设置的话不能预约");
        }
        //  4.预约日期的预约人数已经满了
        Integer reservations =orderSettingMaper.getOrderReservationsByOrderDate(submitOrder.getOrderDate());
        if (reservations >= number){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        // 5.不能重复预约(同一个用户在同一天预约了同一个套餐)
        if (orderMapper.getByIdCardAndOrderDate(submitOrder.getIdCard(),  submitOrder.getOrderDate()) != null ){
            return new Result(false, MessageConstant.HAS_ORDERED);
        }
        // 6.当前用户不是会员,需要自动完成注册
        Member member = memberMapper.findByIdCard(submitOrder.getIdCard());
        if (member == null){
            member = new Member();
            member.setName(submitOrder.getName());
            member.setPhoneNumber(String.valueOf(submitOrder.getTelephone()));
            member.setIdCard(submitOrder.getIdCard());
            member.setSex(submitOrder.getSex());
            member.setRegTime(submitOrder.getOrderDate());
            memberMapper.add(member);
        }
        // 根据submitOrder中的idCard查询会员是否存在
        Order order = new Order(member.getId(), submitOrder.getOrderDate(), null, Order.ORDERSTATUS_NO, submitOrder.getSetmealId());
        orderMapper.add(order);
        // 更新已预约人数
        orderSettingMaper.editReservationByOrderDate(submitOrder.getOrderDate());
        return new Result(true, "预约成功");
    }

}
